<?php

include("../utility/connection.php");
include("../model/mUser.php");
include("../model/mUserSeminar.php");
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $ubah = trim(mysql_real_escape_string($_POST["hidUbah"]));
    if ($ubah == '') {
        // hapus session2 yg gak diperlukan
        unset($_SESSION['ubah']);
        unset($_SESSION['ubahUser']);
        // simpan perubahan
        $Nama = trim(mysql_real_escape_string($_POST["txtNama"]));
        $Alamat = trim(mysql_real_escape_string($_POST["txtAlamat"]));
        $Kota = trim(mysql_real_escape_string($_POST["txtKota"]));
        $Propinsi = trim(mysql_real_escape_string($_POST["txtPropinsi"]));
        $Negara = trim(mysql_real_escape_string($_POST["txtNegara"]));
        $KodePos = trim(mysql_real_escape_string($_POST["txtKodePos"]));
        $Telp = trim(mysql_real_escape_string($_POST["txtTelp"]));
        $Email = trim(mysql_real_escape_string($_POST["txtEmail"]));
        $LoginId = trim(mysql_real_escape_string($_POST["txtLoginId"]));
        $Password = trim(mysql_real_escape_string($_POST["txtPassword"]));

        $countUser = fnCountUser();
        $arrResult = mysql_fetch_array($countUser);
        $JmlUser = $arrResult[0];
        $StsAdmin = 0;
        if ($JmlUser == 0) {
            $StsAdmin = 1;
        }

        fnInsertUser($Nama, $Alamat, $Kota, $Propinsi, $Negara, $KodePos, $Telp, $Email, $LoginId, $Password, $StsAdmin);
        // ambil user id
        $result = fnLogin($LoginId, $Password);
        $arrResult = mysql_fetch_array($result);
        $UserId = $arrResult[0];
        // simpan status user di masing2 seminar
        $JmlSeminar = trim(mysql_real_escape_string($_POST["hidJmlSeminar"]));
//        echo "jml seminar: ".$JmlSeminar;
        if ($JmlSeminar > 0) {
            for ($a = 1; $a <= $JmlSeminar; $a++) {
                $SeminarId = trim(mysql_real_escape_string($_POST["hidIdSeminar_" . $a]));
                $UserGroup = trim(mysql_real_escape_string($_POST["hidUserGroup_" . $a]));
//                echo "seminar id: ".$SeminarId.", user group: ".$UserGroup;
                if ($UserGroup != '') {
                    fnInsertUserSeminar($SeminarId, $UserId, $UserGroup);
                }
            }
        }
        // masuk halaman admin seminar lagi
        unset($_SESSION['userResult']);
        unset($_SESSION['userId']);
        header("location: ../ui/admin/userAdmin.php");
    } else {
        echo "koq di sini..";
    }
}
?>
